import { loginAsync } from '../../api/user'
import axios from 'axios'

const loginModule = {
  state: {
    userInfo: null,
    token: null,
  },
  actions: {
    login (context, value) {
    //   console.log(context)
    //   return axios({
    //     url: 'http://rap2api.taobao.org/app/mock/294500/api/sessions',
    //     method: 'POST',
    //     data: value,
    //   }).then(data => {
    //     console.log('成功', data)
    //     context.commit('loginSuccess', data)
    //     // return true // 返回给login页面 表示正确
    //   }).catch(err => console.log(err))

      return loginAsync(value)
        .then(data => {
          console.log('成功', data)
          context.commit('loginSuccess', data)
          return true
        })
    },
  },
  mutations: {
    loginSuccess (state, value) {
      state.userInfo = value.userInfo
      state.token = value.token
      console.log(state)
    },
    loginOut (state) {
      state.userInfo = null
      state.token = null
    },
  },
}

export default loginModule
